package com.haibei.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.common.cache.SimpleImgLoaderLru;
import com.haibei.R;
import com.haibei.adapter.bean.Goods;
import com.haibei.cache.list.ImageManagerLru;

public class GoodsAdapter extends BaseAdapter {

	private ArrayList<Goods> mArrayList;
	private Context mContext;

	public GoodsAdapter(Context context, ArrayList<Goods> list) {
		this.mContext = context;
		this.mArrayList = list;
	}

	@Override
	public int getCount() {
		return mArrayList.size();
	}

	@Override
	public Object getItem(int position) {
		return mArrayList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vl = null;
		if (null == convertView) {
			vl = new ViewHolder();
			convertView = View.inflate(mContext, R.layout.item_goods_item, null);
			vl.imageView = (ImageView) convertView.findViewById(R.id.imageView);
			vl.textViewName = (TextView) convertView.findViewById(R.id.textViewName);
			vl.textViewNowPrice = (TextView) convertView.findViewById(R.id.textViewNowPrice);
			vl.textViewOrginPrice = (TextView) convertView.findViewById(R.id.textViewOrginPrice);
			vl.textViewSeldCount = (TextView) convertView.findViewById(R.id.textViewSeldCount);
			vl.textViewCollect = (TextView) convertView.findViewById(R.id.textViewCollectCount);
			convertView.setTag(vl);
		} else {
			vl = (ViewHolder) convertView.getTag();
		}
		Goods gods = mArrayList.get(position);
		vl.textViewName.setText(gods.getTitle());
		vl.textViewNowPrice.setText("￥" + gods.getSalePrice());
		if (gods.isPromotion()) {
			vl.textViewOrginPrice.setVisibility(View.VISIBLE);
			vl.textViewOrginPrice.setText("￥"+gods.getMarketPrice());
			vl.textViewOrginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
//			textview.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
		} else {
			vl.textViewOrginPrice.setVisibility(View.INVISIBLE);
		}
		vl.textViewSeldCount.setText("已售:" + gods.getSales());
		vl.textViewCollect.setText("收藏:" + gods.getFavoriteCount());
		SimpleImgLoaderLru.from(mContext).displayImage(vl.imageView, gods.getThumbnailImg(), R.drawable.bg_default_item_img);
//		ImageManagerLru.from(mContext).displayImage(vl.imageView, gods.getThumbnailImg(), R.drawable.bg_default_item_img);
		return convertView;
	}

	class ViewHolder {
		ImageView imageView;
		TextView textViewName;
		TextView textViewNowPrice;
		TextView textViewOrginPrice;
		TextView textViewSeldCount;
		TextView textViewCollect;
	}
}
